/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ibmmodel3;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 *
 * @author mwh
 */
public class VocabularyTableCache implements VocabularyTable{

    VocabularyTable table;

    Map<String, Integer> cache;


    public VocabularyTableCache(VocabularyTable table, int size) {
        this.table = table;

        final long maxsize = Math.round(size * 0.80);

        cache = new LinkedHashMap<String, Integer>(size, 0.75F, true) {

            @Override
            protected boolean removeEldestEntry(Map.Entry<String, Integer> eldest) {

                return size() > maxsize;
            }
        };
    }


    public int get(String word) {
        Integer w = cache.get(word);
        if (w == null) {
            w = table.get(word);
            cache.put(word, w);
        }
        return w;
    }
}
